package com.example.qu_tao.database;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.qu_tao.entity.Goods;
import com.example.qu_tao.entity.Photo;

import java.util.ArrayList;
import java.util.List;

public class GoodsDao {
    private DBHelper dbHelper;
    private SQLiteDatabase db;


    // 定义列名常量，与 DB 表结构保持一致
    private static final String COLUMN_GOODS_ID = "goods_id";
    private static final String COLUMN_GOODS_NAME = "goods_name";
    private static final String COLUMN_GOODS_PRICE = "goods_price";
    private static final String COLUMN_GOODS_FREIGHT = "goods_freight";
    private static final String COLUMN_GOODS_ADDRESS = "goods_address";
    private static final String COLUMN_GOODS_UNIT = "goods_unit";
    private static final String COLUMN_GOODS_NUMBER = "goods_number";
    private static final String COLUMN_GOODS_CONTENT = "goods_content";
    // 其他列名...

    public GoodsDao(Context context) {
        dbHelper = new DBHelper(context);
        db = dbHelper.getWritableDatabase();
    }

    public List<Goods> getAllGoods() {
        List<Goods> goodsList = new ArrayList<>();
        // 使用 DBHelper 暴露的表名
        Cursor cursor = db.query(
                DBHelper.TABLE_NAME_GOODS,
                null,
                null,
                null,
                null,
                null,
                "goods_id DESC"
        );

        if (cursor != null && cursor.moveToFirst()) {
            do {
                Goods goods = new Goods();
                int goodsIdIndex = cursor.getColumnIndex("goods_id");
                if (goodsIdIndex != -1) {
                    goods.setGoods_id(cursor.getInt(goodsIdIndex));
                }
                int goodsNameIndex = cursor.getColumnIndex("goods_name");
                if (goodsNameIndex != -1) {
                    goods.setGoods_name(cursor.getString(goodsNameIndex));
                }
                int goodsPriceIndex = cursor.getColumnIndex("goods_price");
                if (goodsPriceIndex != -1) {
                    goods.setGoods_price(cursor.getDouble(goodsPriceIndex));
                }
                int goodsFreightIndex = cursor.getColumnIndex("goods_freight");
                if (goodsFreightIndex != -1) {
                    goods.setGoodsFreight(cursor.getDouble(goodsFreightIndex));
                }
                int goodsAddressIndex = cursor.getColumnIndex("goods_address");
                if (goodsAddressIndex != -1) {
                    goods.setGoodsAddress(cursor.getString(goodsAddressIndex));
                }
                int unitIndex = cursor.getColumnIndex("goods_unit");
                if (unitIndex != -1) {
                    goods.setUnit(cursor.getString(unitIndex));
                }
                int goodsNumberIndex = cursor.getColumnIndex("goods_number");
                if (goodsNumberIndex != -1) {
                    goods.setGoods_number(cursor.getInt(goodsNumberIndex));
                }
                int goodsContentIndex = cursor.getColumnIndex("goods_content");
                if (goodsContentIndex != -1) {
                    goods.setGoods_content(cursor.getString(goodsContentIndex));
                }

                // 其他列的赋值...

                // 获取商品图片（关联 photo 表）
                List<Photo> photoList = getGoodsPhotos(goods.getGoods_id());
                goods.setPhotoList(photoList);

                goodsList.add(goods);
            } while (cursor.moveToNext());
            cursor.close();
        }
        return goodsList;
    }

    // 其他方法（如 getGoodsById、getGoodsPhotos）同理，处理 getColumnIndex 的返回值
    @SuppressLint("Range")
    public Goods getGoodsById(int goodsId) {
        Goods goods = null;
        Cursor cursor = db.query(
                DBHelper.TABLE_NAME_GOODS,
                null,
                "goods_id=?",
                new String[]{String.valueOf(goodsId)},
                null,
                null,
                null
        );
        //    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_GOODS + " (" +
//              "goods_id integer primary key autoincrement," +
//              "goods_name text," +
//              "goods_price double," +
//              "goods_freight double," +
//              "goods_address text," +
//              "goods_unit text," +
//              "goods_number integer," +
//              "goods_content text" +
//              ")");
        try {
            if (cursor != null && cursor.moveToFirst()) {
                goods = new Goods();
                // 使用 getInt 和 getString 获取列值
                goods.setGoods_id(cursor.getInt(cursor.getColumnIndex(COLUMN_GOODS_ID)));
                goods.setGoods_name(cursor.getString(cursor.getColumnIndex(COLUMN_GOODS_NAME)));
                goods.setGoods_price(cursor.getDouble(cursor.getColumnIndex(COLUMN_GOODS_PRICE)));
                goods.setGoodsFreight(cursor.getDouble(cursor.getColumnIndex(COLUMN_GOODS_FREIGHT)));
                goods.setGoodsAddress(cursor.getString(cursor.getColumnIndex(COLUMN_GOODS_ADDRESS)));
                goods.setUnit(cursor.getString(cursor.getColumnIndex(COLUMN_GOODS_UNIT)));
                goods.setGoods_number(cursor.getInt(cursor.getColumnIndex(COLUMN_GOODS_NUMBER)));
                goods.setGoods_content(cursor.getString(cursor.getColumnIndex(COLUMN_GOODS_CONTENT)));
                // 获取商品图片
                List<Photo> photoList = getGoodsPhotos(goods.getGoods_id());
                goods.setPhotoList(photoList);
            }
        } finally {
            // 确保游标被关闭
            if (cursor != null) {
                cursor.close();
            }
        }
        return goods;
    }
    private List<Photo> getGoodsPhotos(int goodsId) {
        List<Photo> photoList = new ArrayList<>();
        Cursor cursor = db.query(
                DBHelper.TABLE_NAME_PHOTO,
                null,
                "goods_img_id=?",
                new String[]{String.valueOf(goodsId)},
                null,
                null,
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
            do {
                Photo photo = new Photo();
                // 调用 Photo 类正确的 setImgId 方法
                int imgIdIndex = cursor.getColumnIndex("img_id");
                if (imgIdIndex != -1) {
                    photo.setImgId(cursor.getInt(imgIdIndex));
                }

                // 调用 Photo 类正确的 setGoodsImgId 方法
                int goodsImgIdIndex = cursor.getColumnIndex("goods_img_id");
                if (goodsImgIdIndex != -1) {
                    photo.setGoodsImgId(cursor.getInt(goodsImgIdIndex));
                }

                int imageIndex = cursor.getColumnIndex("image");
                if (imageIndex != -1) {
                    photo.setData(cursor.getBlob(imageIndex));
                }

                photoList.add(photo);
            } while (cursor.moveToNext());
            cursor.close();
        }
        return photoList;
    }

    public void close() {
        if (db != null && db.isOpen()) {
            db.close();
        }
    }
}